package comp.wxapi.controller;

import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import comp.wxapi.bean.MiniQrcode;
import comp.wxapi.service.MiniappUserService;
import me.chanjar.weixin.common.error.WxErrorException;

@RestController
@RequestMapping(value="wxapi/mini")
public class MiniController {
	
	private static final Logger logger = LoggerFactory.getLogger(MiniController.class);
	
	@Autowired
	private MiniappUserService miniappUserService;
	
	
	@PostMapping(path="getwxacodeunlimit")
	public void getwxacodeunlimit(HttpServletRequest request,HttpServletResponse response
			,@RequestBody MiniQrcode miniQrcode) throws WxErrorException {
		FileInputStream fStream = null;
		try {
			response.setCharacterEncoding("UTF-8");
			ServletOutputStream outputStream = response.getOutputStream();
			fStream = miniappUserService.qrcode(miniQrcode, "wxacodeunlimit");
			outputStream.write(IOUtils.toByteArray(fStream));
		} catch (IOException e) {
			logger.error("ERROR:", e);
		} finally {
			IOUtils.closeQuietly(fStream);
		}
	}
	
	@PostMapping(path="getwxacode")
	public void getwxacode(HttpServletRequest request,HttpServletResponse response
			,@RequestBody MiniQrcode miniQrcode) throws WxErrorException {
		FileInputStream fStream = null;
		try {
			response.setCharacterEncoding("UTF-8");
			ServletOutputStream outputStream = response.getOutputStream();
			fStream = miniappUserService.qrcode(miniQrcode, "wxacode");
			outputStream.write(IOUtils.toByteArray(fStream));
		} catch (IOException e) {
			logger.error("ERROR:", e);
		} finally {
			IOUtils.closeQuietly(fStream);
		}
	}
	
	@PostMapping(path="getwxaqrcode")
	public void getwxaqrcode(HttpServletRequest request,HttpServletResponse response
			,@RequestBody MiniQrcode miniQrcode) throws WxErrorException {
		FileInputStream fStream = null;
		try {
			response.setCharacterEncoding("UTF-8");
			ServletOutputStream outputStream = response.getOutputStream();
			fStream = miniappUserService.qrcode(miniQrcode, "wxaqrcode");
			outputStream.write(IOUtils.toByteArray(fStream));
		} catch (IOException e) {
			logger.error("ERROR:", e);
		} finally {
			IOUtils.closeQuietly(fStream);
		}
	}
	
	public static void main(String[] args) {
		System.out.println(Integer.MIN_VALUE);
	}
}
